/**
 * @File Name: httpconn.h
 * @brief  HttpConn 客户端连接类
 * @Author : beyon_sir email:caoboyang.chn@gmail.com
 * @Version : 1.0
 * @Create Date : 2022-11-21
 * 
 */
#ifndef HTTP_CONN_H
#define HTTP_CONN_H

#include <sys/types.h>
#include <sys/uio.h>     // readv/writev
#include <arpa/inet.h>   // sockaddr_in
#include <stdlib.h>      // atoi()
#include <errno.h>      

#include "../log/log.h"
#include "../pool/sqlconnRAII.h"
#include "../buffer/buffer.h"
#include "httprequest.h"
#include "httpresponse.h"

class HttpConn {
public:
    HttpConn();

    ~HttpConn();

    void init(int sockFd, const sockaddr_in& addr);

    ssize_t read(int* saveErrno);

    ssize_t write(int* saveErrno);

    void Close();

    int GetFd() const;

    int GetPort() const;

    const char* GetIP() const;
    
    sockaddr_in GetAddr() const;
    
    bool process();

    int ToWriteBytes() { 
        return iov_[0].iov_len + iov_[1].iov_len; 
    }

    bool IsKeepAlive() const {
        return request_.IsKeepAlive();
    }

    static bool isET;
    static const char* srcDir;         // 资源的目录
    static std::atomic<int> userCount; // 总共客户端的连接数量 
    // std::atomic<T> 模板类可以使对象操作为原子操作，避免多线程竞争问题
    // cited from: https://blog.csdn.net/u013288190/article/details/119868240
    
private:
   
    int fd_;
    struct  sockaddr_in addr_;

    bool isClose_;
    
    int iovCnt_;
    struct iovec iov_[2]; // HTTP response 中的响应头 和响应体
    
    Buffer readBuff_;   // 读缓冲区 保存请求数据的内容
    Buffer writeBuff_;  // 写缓冲区 保存相应数据的内容

    HttpRequest request_;
    HttpResponse response_;
};


#endif //HTTP_CONN_H